package delta;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;

import ViewPackage.TowerView;

public class Tower implements IFieldable {

	
	private ArrayList<Road> roads = new ArrayList<Road>();
	public Field place;
	private boolean[] ms = new boolean[4];
	private Integer damage1;
	private Integer damage2;
	private Integer distance;
	private Integer distanceOld;
	private Integer frequency;
	private Integer frequencySteps;
	private Boolean isFoggy = false;
	private TowerView tView=null;
	
	public Tower(Field field) {
		
		
		place=null;
		//Nem engedi lerakni roadra
		if(field!=null && field.getDistance()==-1){
			//Eloszor ellenorizni kell, hogy van -e torony a mezon
			boolean vanTower=false;
			for(IFieldable obj:field.getObjects()){
				if(obj.action(null)==false)vanTower=true;
			}
			if(!vanTower){
				place = field;
				place.addObject(this);
				
				damage1 = 5;
				damage2 = 5;
				frequency = 2;
				frequencySteps = 0;
				distance = 2;
				
				Player.decreaseWitchcraft(25);
				findRoads(place, distance);
			}
			
		}
	}

	/*
	 * rekurziv fuggveny mely megkeresi a hatotavolsagon belulre eso utakat es
	 * feltolti roads attributumba
	 */
	private void findRoads(Field field, int range) {
		if (range == 0) {
			return;
		}
		ArrayList<Field> list = field.getSurroundingFields();
		for (Field f : list) {
			if (f != null) {
				if (f.getDistance() != -1) {
					Road r = (Road) f;
					if (roads != null && roads.contains(r) == false)
						roads.add(r);
				}
				findRoads(f, range - 1);
			}
		}

	}

	public Boolean AddMagicStone(MagicStoneToTower ms) {

		if (ms.getRangeMultiplier() != null) {
			if (this.ms[0] == false) {
				this.ms[0] = true;
				roads = new ArrayList<Road>();
				distance=(int)Math.round(ms.getRangeMultiplier());
				findRoads(place, distance);
			} else
				return false;
		}
		else if (ms.getDamageMultiplier1() != null) {
			if (this.ms[1] == false) {
				this.ms[1] = true;
				damage1 = (int) (damage1 * ms.getDamageMultiplier1());
			} else
				return false;
		}
		else if (ms.getDamageMultiplier2() != null) {
			if (this.ms[2] == false) {
				this.ms[2] = true;
				damage2 = (int) (damage2 * ms.getDamageMultiplier2());
			} else
				return false;
		}
		else if (ms.getFrequencyMultiplier() != null) {
			if (this.ms[3] == false) {
				this.ms[3] = true;
				frequency = (int) (frequency * ms.getFrequencyMultiplier());
			} else
				return false;
		}

		return true;

	}

	public void update() {
		if (isFoggy)
			return;
		// Initialize the attack parameters
		ActionParameters params = new ActionParameters();
		params.setDamageDE(damage1);
		params.setDamageHH(damage2);
		
		if(frequencySteps==null || frequency==null){
			if(place==null)System.out.println("nincs mezo megis hozzadtam vhogy");
			else{
				System.out.println("nem adtam hozza mezohoz se");
			}
		}
		if (frequencySteps == frequency - 1) {

			boolean wasAction = false;
			Collections.sort(roads,new RoadComparator());
			for (Road road : roads) {

				ArrayList<IFieldable> list = road.getObjects();
				for (IFieldable f : list) {
					if (f.action(params)) {
						wasAction = true;
						//A view ertesitese
						if(tView!=null)tView.shootTarget(f);
						break;
					}

				}
				if (wasAction) {
					break;
				}

			}
			frequencySteps = 0;
		} else {
			frequencySteps++;
		}

	}

	public String toString() {
		StringBuilder str = new StringBuilder();
		str.append("Tower:\n");
		for (int i = 0; i < ms.length; i++) {
			str.append("Ms[" + i + "]=" + ms[i] + "\n");
		}
		str.append("range="+distance+"\n");
		str.append("\n");
		return str.toString();

	}

	@Override
	public boolean action(ActionParameters params) {
		//Ahhoz kellet a view irasakor, hogy ne lehessen tobb tornyot lerakni egy mezore
		//ezt a modell megengedi
		if(params==null)return false;
		
		// kod
		if (params.isFoggy() == true) {
			this.isFoggy = true;
			distanceOld = distance;
			distance = 1;
		} else if (params.isFoggy() == false) {
			this.isFoggy = false;
			distance = distanceOld;
		}
		return false;
	}

	@Override
	public String getMapchar() {
		// TODO Auto-generated method stub

		return new String("T");
	}

	@Override
	public Field getField() {
		// TODO Auto-generated method stub
		return place;
	}
	
	public void addTowerView(TowerView tView){
		this.tView=tView;
	}
	
}

class RoadComparator implements Comparator<Road> {

	@Override
	public int compare(Road r1, Road r2) {
		int dis1 = r1.getDistance();
		int dis2 = r2.getDistance();
		return dis1 > dis2 ? 1 : dis2 == dis1 ? 0 : -1;
	}

}
